FABRIC_SRC_DIR = /opt/gopath/src/github.com/hyperledger/
artifact: clean
	cp -rf ./e2e_cli $(FABRIC_SRC_DIR)/fabric/examples/
	cd $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && ./generateArtifacts.sh mychannel

orderer: 
	cd $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && docker-compose -f docker-compose-cli-orderer.yaml up -d
	docker ps -a

org1: clean cli
	cd $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && docker-compose -f docker-compose-cli-peer-org1.yaml up -d

org2: clean cli
	cd $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && docker-compose -f docker-compose-cli-peer-org2.yaml up -d

cli:
	rm -rf $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && cp -rf ./e2e_cli.tar.gz $(FABRIC_SRC_DIR)/fabric/examples/
	cd $(FABRIC_SRC_DIR)/fabric/examples/ && tar -zxvf e2e_cli.tar.gz && chmod -R +x ./e2e_cli/*

down:
	#docker stop $(docker ps -a -q)
	#docker rm $(docker ps -a -q)

test:
	docker exec -it cli bash && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}' && \
	peer chaincode invoke -o orderer.example.com:7050  --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem  -C mychannel -n mycc -c '{"Args":["invoke","a","b","20"]}' && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'

tar:
	rm -rf ./e2e_cli.tar.gz $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli.tar.gz
	cd $(FABRIC_SRC_DIR)/fabric/examples/ && tar -zcvf ./e2e_cli.tar.gz ./e2e_cli
	cp -f $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli.tar.gz ./

clean:
	rm -rf $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli.tar.gz
	
.PHONY: artifact orderer org1 org2 down test tar clean
